<?php
/**
 * Groups module for XOOPS
 *
 * You may not change or alter any portion of this comment or credits
 * of supporting developers from this source code or any supporting source code 
 * which is considered copyrighted (c) material of the original comment or credit authors.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *
 * @copyright       The Groups Project http://code.google.com/p/groupsforxoops/
 * @license         http://www.fsf.org/copyleft/gpl.html GNU public license
 * @package         groups
 * @since           2.3.0
 * @author          Hui Xiao < xh.8326@gmail.com >
 * @version         $Id: category.php Sat Jul 18 12:44:39 CST 2009
 */

include "conf.inc";

$cat_id = isset($_GET["id"]) ? intval($_GET["id"]) : 0 ;
$start = isset($_GET["start"]) ? intval($_GET["start"]) : 0;

$op = isset($_GET["op"]) ? trim($_GET["op"]) : "user";

$category_handler = xoops_getmodulehandler("category");
$group_handler = xoops_getmodulehandler("group");

$xoopsOption["xoops_pagetitle"] = $xoopsModuleConfig["groupstitle"];
$xoopsOption["template_main"] = "groups_category.html";
include "header.php";

$limit = 20 ;
$extra = "";
$params = array(
	"criteria" => array(
//		"group_status" => 1
	),
	"start" => $start,
	"limit" => $limit,
	"sort"  => "group_member_total",
	"order" => "DESC"
);
if ( !empty($cat_id) ) {
	$params["criteria"]["cat_id"] = $cat_id;
	$cat_obj = $category_handler->get($cat_id);
	$extra .= "id={$cat_id}";
}

switch ($op) {
	case "topic":
		$params["sort"] = "group_topic_total";
		$extra .= "&op=topic";
		break;
	case "foot":
		$params["sort"] = "group_foot_total";
		$extra .= "&op=foot";
		break;
	case "time":
		$params["sort"] = "group_create_time";
		$extra .= "&op=time";
		break;
		
	default:
		$params["sort"] = "group_member_total";
}
$params["extra"] = $extra;
$objs = $group_handler->getGroups($params);
$pagenav = $group_handler->getPageNav($params);

$user_ids = array();
if ( !empty($objs) ) {
	foreach ( $objs as $k=>$obj ) {
		$user_ids[] = $obj->getVar("uid");
	}
	$user_ids = array_unique($user_ids);
}

if ( $user_ids ) {
	$users = xoGroups::getUsers($user_ids);
}

$groups = array();
if ( !empty($objs) ) {
	foreach ( $objs as $k=>$obj ) {
		$groups[$k] = $obj ->getValues();
		$groups[$k]["author_nickname"] =  $obj->getVar("uid") ? $users[$obj->getVar("uid")]["name"] : "";
		$groups[$k]["createtime"] =  $obj->createTime();
		$groups[$k]["logo"] =  $obj->logo();
	}
}

$_cat = $category_handler->getCatSelect();
include_once(dirname(__FILE__)."/include/form.search.php");

$xoopsTpl->assign( array(
	"category"=> ( !empty($cat_obj) && is_object($cat_obj) ) ? $cat_obj->getValues() : "",
	"categories"=>$_cat,
	"groups"=>$groups,
	"pagenav"=>$pagenav) );

include "footer.php";
?>